﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;
using UDS.WebApp;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// DisplayTacheMember 的摘要说明。
    /// </summary>
    public partial class DisplayTacheMemberPage : Page
    {
        private int _docId;
        private int _flowId;
        private int _stepId;

        protected int ReturnPage
        {
            get;
            private set;
        }

        protected FlowMemberDisplayType DisplayType
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            _docId = QSUtil.GetPositiveInt32("DocID");
            _flowId = QSUtil.GetPositiveInt32("FlowID");
            _stepId = QSUtil.GetPositiveInt32("StepID");
            DisplayType = (FlowMemberDisplayType)QSUtil.GetPositiveInt32("DisplayType");
            ReturnPage = QSUtil.GetInt32("ReturnPage", 1);

            if (_docId > 0)
            {
                linkAllMember.NavigateUrl = FormatUrl(FlowMemberDisplayType.AllMembers);
                linkReceiver.NavigateUrl = FormatUrl(FlowMemberDisplayType.Receiver);
                linkSigned.NavigateUrl = FormatUrl(FlowMemberDisplayType.Signed);
                linkUnsigned.NavigateUrl = FormatUrl(FlowMemberDisplayType.Unsigned);
                linkPostiler.NavigateUrl = FormatUrl(FlowMemberDisplayType.Postiler);
            }
            else
            {
                linkAllMember.Enabled = false;
                linkReceiver.Enabled = false;
                linkSigned.Enabled = false;
                linkUnsigned.Enabled = false;
                linkPostiler.Enabled = false;

                linkAllMember.NavigateUrl = "#";
                linkReceiver.NavigateUrl = "#";
                linkSigned.NavigateUrl = "#";
                linkUnsigned.NavigateUrl = "#";
                linkPostiler.NavigateUrl = "#";
            }

            // Bind data.
            BindGrid();
            base.OnInit(e);
        }

        private string FormatUrl(FlowMemberDisplayType displayType)
        {
            return string.Concat("DisplayTacheMember.aspx?DocID=", _docId.ToString(), "&DisplayType=", ((int)displayType).ToString(), "&ReturnPage=", ReturnPage.ToString());
        }

        private void BindGrid()
        {
            IList<StaffInfo> list = null;
            switch (DisplayType)
            {
                case FlowMemberDisplayType.AllMembers:
                    list = (_docId > 0) ? DocumentFlow.GetStaffInStep(_docId) : DocumentFlow.GetStaffInStep(_flowId, _stepId);
                    break;

                case FlowMemberDisplayType.Receiver:
                    list = DocumentFlow.GetReceiver(_docId);
                    break;

                case FlowMemberDisplayType.Signed:
                    list = DocumentFlow.GetSignedList(_docId);
                    break;

                case FlowMemberDisplayType.Unsigned:
                    list = DocumentFlow.GetUnsignedList(_docId);
                    break;

                case FlowMemberDisplayType.Postiler:
                    list = DocumentFlow.GetPostiler(_docId);
                    break;
            }

            dbStaffList.DataSource = list;
            dbStaffList.DataBind();
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}